/*******************************************************************************
 * Copyright 2011 Towee.net
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package net.towee.server.ftp;

import java.io.File;

import javax.inject.Inject;

import net.towee.server.CoreSettings;

import org.apache.ftpserver.ConnectionConfigFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.command.CommandFactory;
import org.apache.ftpserver.ftplet.FileSystemFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.ssl.SslConfigurationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.name.Named;

/**
 * This wrapper manipulate with stored Nodes and Documents. It
 * not call to filesystem. It work with towee stores.
 * 
 * This implementation uses Apache FtpServer library to provide
 * full-futured ftp server.
 * 
 * @author viiv.c58@gmail.com (Artem Artem'ev)
 */
public class ToweeFtpServer {
	private static final Logger LOG = LoggerFactory.getLogger(ToweeFtpServer.class);
	
	private final UserManager userManager;
	private final CommandFactory commandFactory;
	private final FileSystemFactory fsFactory;
	
	private final int ftpPort;
	
	@Inject
	public ToweeFtpServer(@Named(CoreSettings.FTP_PORT) int ftpPort,
			UserManager ftpUserManager, CommandFactory commandFactory,
			FileSystemFactory fsFactory) {
		this.userManager = ftpUserManager;
		this.commandFactory = commandFactory;
		this.ftpPort = ftpPort;
		this.fsFactory = fsFactory;
	}
	
	/**
	 * This method start the server 
	 */
	public void start() {
        FtpServerFactory serverFactory = new FtpServerFactory();
        ListenerFactory factory = new ListenerFactory();
                
        // set the port of the listener
        factory.setPort(ftpPort);

        // define SSL configuration
        SslConfigurationFactory ssl = new SslConfigurationFactory();
        ssl.setKeystoreFile(new File("toweecert.jks"));
        ssl.setKeystorePassword("vcyddbdc");

        // set the SSL configuration for the listener
        factory.setSslConfiguration(ssl.createSslConfiguration());
        factory.setImplicitSsl(false);

        // replace the default listener
        serverFactory.addListener("default", factory.createListener());
        
        // Set userManager
        serverFactory.setUserManager(userManager);
        
        // Set embaded command factory
        serverFactory.setCommandFactory(commandFactory);
        
        // Set towee file system wrapper
        serverFactory.setFileSystem(fsFactory);
        
        // Create config
        ConnectionConfigFactory cfg = new ConnectionConfigFactory();
        cfg.setAnonymousLoginEnabled(false);
        cfg.setMaxLoginFailures(2);
        cfg.setMaxThreads(1000);
        cfg.setMaxLogins(5);
        
        // Set config
        serverFactory.setConnectionConfig( cfg.createConnectionConfig() );
        
        // Create and start the server
        FtpServer server = serverFactory.createServer();

        try {
			server.start();
		} catch (FtpException e) {
			LOG.info("FTP server start filed.", e);
		}
	}
}
